package com.ereport.controller.loginController;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.ExecutiveDAO;
import com.ereport.dao.LoginDetDAO;
import com.ereport.entity.Executives;
import com.ereport.entity.LoginDet;



@Controller
@RequestMapping("/user")
public class LoginController {
	@RequestMapping(value="/login.do",method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest req, ModelAndView model,HttpSession session) {
		System.out.println("hello");
		String userid=req.getParameter("userEmail");
		String pass=req.getParameter("pwd");
		ExecutiveDAO eDao=new ExecutiveDAO();
		System.out.println("in x0029 searching  CompanyUserid " + userid + " pass  "
				+ pass);
		try {
			
			 Executives e=eDao.validatCredentials(userid,pass);
			 System.out.println("executive object is "+e);
			 if(e!=null && e.getExeEmail().trim().length()>0){
								
				session.setAttribute("loginUser", e);
				System.out.println("session is "+session.getAttribute("loginUser"));
				LoginDetDAO ldDao=new LoginDetDAO();
				LoginDet ld=ldDao.getLastLogin(e.getId());
				session.setAttribute("lastLogin",ld);
				java.util.Date date= new java.util.Date();
				System.out.println("exe id "+e.getId());
				ldDao.setLastLogin(e.getId(),new Timestamp(date.getTime()));
				if(e.isIs_admin()==true){
					model.setViewName("/jsp/admin/adminPage.jsp");
				}else {
				model.setViewName("/jsp/executivePage.jsp");
				}
				model.addObject("exeName",e.getExeName());
				if(ld!=null){
				model.addObject("lastLogin",ld.getLastLogin());
				}else{
					model.addObject("lastLogin","This is 1st Login");
				}
				return model;
			}
			else {
				
				model.addObject("error", "The Username or Password you entered is incorrect");
				model.setViewName("/jsp/login.jsp");
				
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;

	}
	@RequestMapping(value="/logout")
	public ModelAndView logout(HttpServletRequest request,HttpSession session,ModelAndView model) {
		       session = request.getSession(false);
		        if (session != null) {
		            session.invalidate();
		        }
		       
		        model.setViewName("/jsp/login.jsp");
		return model;
      }
	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		LoginDet ld=(LoginDet)session.getAttribute("lastLogin");
		if(ex.isIs_admin()==true){
			model.setViewName("/jsp/admin/adminPage.jsp");
		}else {
		model.setViewName("/jsp/executivePage.jsp");
		}
		model.addObject("exeName",ex.getExeName());
		if(ld!=null){
		model.addObject("lastLogin",ld.getLastLogin());
		}else{
			model.addObject("lastLogin","This is 1st Login");
		}
		return model;
	}
}